package com.rescue.controller.user;

import com.rescue.annotation.GlobalInterceptor;
import com.rescue.enumeration.OperationFrequencyType;
import com.rescue.forum.dto.PublishArticleDTO;
import com.rescue.forum.dto.QueryArticleDTO;
import com.rescue.forum.dto.SearchDTO;
import com.rescue.forum.vo.ArticleDetailVO;
import com.rescue.forum.vo.DownloadFileVO;
import com.rescue.result.PageResult;
import com.rescue.result.Result;
import com.rescue.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user/forum/article")
@RestController("userArticleController")
@Api(tags = "用户文章相关接口")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //@GlobalInterceptor(frequency = OperationFrequencyType.POST_ARTICLE)
    @ApiOperation("发布文章")
    @PostMapping("/save")
    public Result save(@RequestBody PublishArticleDTO publishArticleDTO) {
        log.info("发布文章{}", publishArticleDTO);
        articleService.save(publishArticleDTO);
        return Result.success();
    }

    /**
     * 获取文章列表
     *
     * @param queryArticleDTO
     * @return
     */
    @ApiOperation("获取文章列表")
    @GetMapping("/list")
    public Result<PageResult> list(QueryArticleDTO queryArticleDTO) {
        log.info("获取文章列表{}", queryArticleDTO);
        PageResult pageResult = articleService.list(queryArticleDTO);
        return Result.success(pageResult);
    }

    /**
     * 获取文章详情
     *
     * @param articleId
     * @return
     */
    @ApiOperation("获取文章详情")
    @GetMapping("/getDetail")
    public Result<ArticleDetailVO> getDetail(String articleId) {
        log.info("获取文章详情{}", articleId);
        ArticleDetailVO articleDetailVO = articleService.getDetail(articleId);
        return Result.success(articleDetailVO);
    }

    /**
     * 搜索文章
     *
     * @param searchDTO
     * @return
     */
    @ApiOperation("搜索文章")
    @GetMapping("/search")
    public Result<PageResult> search(SearchDTO searchDTO) {
        log.info("搜索文章{}", searchDTO);
        PageResult p = articleService.search(searchDTO);
        return Result.success(p);
    }

    /**
     * 点赞/取消点赞
     *
     * @param articleId
     * @param type
     * @return
     */
    @GlobalInterceptor(frequency = OperationFrequencyType.DO_LIKE)
    @ApiOperation("点赞/取消点赞")
    @GetMapping("/doLike")
    public Result doLike(String articleId, Integer type) {
        log.info("点赞/取消点赞");
        articleService.doLike(articleId, type);
        return Result.success();
    }

    /**
     * 下载文件
     *
     * @param fileId
     * @return
     */
    @ApiOperation("下载文件")
    @GetMapping("/download")
    public Result<DownloadFileVO> download(String fileId) {
        log.info("下载文件");
        DownloadFileVO downloadFileVO = articleService.download(fileId);
        return Result.success(downloadFileVO);
    }
}
